<template>
  <view class="pages">
    <h-nav-bar-yun fixed :title="barTitle" :rightWidth="rightWidth" :leftWidth="leftWidth" @clickLeft="toBack">
      <template #right>
        <view class="save_btn" @click="submit">保存</view>
      </template>
    </h-nav-bar-yun>
    <view class="textarea_box">
      <up-textarea
          border="none"
          height="170"
          placeholder-class="textarea_placeholder"
          class="evaluation_conant"
          v-model="comment"
          count
          maxlength="500"
          :placeholder="placeholder"
        ></up-textarea>
    </view>
    <h-modal :show="bindShow" text="是否保存编辑内容" leftColor="#5C5C5C" rightColor="#00C1B9" leftBtn="不保存" rightBtn="保存" @clear="navigateBack()" @bind="submit"></h-modal>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { navigateBack, navigateTo, formatDate, showToast } from '@/utils'
import { onLoad, onPageScroll, onShow, onReady } from '@dcloudio/uni-app'
import { commonToolsAddMessageConfig,commonToolsUpdateMessageConfig } from '@/api/suggest'
const placeholder = ref('请输入常用建议，切记不要输入QQ、微信、手机号等联系方式或广告内容')
const rightWidth = ref(`160rpx`)
const leftWidth = ref(`160rpx`)
//nav文本
const barTitle = ref('')
//保存参数
const comment = ref("")
const bindShow=ref(false)
const editId=ref("")
const isUpdate=ref(false)
onLoad((o)=>{
  if(o.title){
    barTitle.value = o.title
    if(o?.title == '添加常用建议'){
      placeholder.value = '请输入常用建议，切记不要输入QQ、微信、手机号等联系方式或广告内容'
    }else{
      placeholder.value = '请输入常用回复，切记不要输入QQ、微信、手机号等联系方式或广告内容'
    }
  }
  if(o.content){
    comment.value = o.content
  }
  if(o.id){
    editId.value = o.id
  }
})
function toBack(){
	if(comment.value){
		bindShow.value=true
	}else{
		navigateBack()
	}
}
async function submit(){
  if(comment.value){
    if(barTitle.value=='添加常用建议'){
		const res=await commonToolsAddMessageConfig({
			content:comment.value,
			type:1
		})
		uni.$emit('addMessage', {id: res.data.id,content:comment.value})
    }else if(barTitle.value=='添加常用回复'){
      const res=await commonToolsAddMessageConfig({
        content:comment.value,
        type:2
      })
      uni.$emit('addMessage', {id: res.data.id,content:comment.value})
    }else if(barTitle.value=='编辑常用建议'){
      const res=await commonToolsUpdateMessageConfig({
        content:comment.value,
        id:editId.value
      })
      uni.$emit('editMessage', {id: editId.value,content:comment.value})
    }else if(barTitle.value=='编辑常用回复'){
      const res=await commonToolsUpdateMessageConfig({
        content:comment.value,
        id:editId.value
      })
      uni.$emit('editMessage', {id: editId.value,content:comment.value})
    }
    navigateBack()
  }else{
    showToast('请输入内容')
  }
}
</script>

<style lang="scss" scoped>
.pages{
  width: 750rpx;
  height: 100vh;
  background: #F5F6F8;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-family: PingFangSC, PingFang SC;
  position: relative;
  .textarea_box{
    width: 702rpx;
    height: 400rpx;
    background: #FFFFFF;
    border-radius: 20rpx;
    margin-top: 20rpx;
    .evaluation_conant{
      padding:24rpx;
      height:352rpx;
    }
  }
}
.save_btn{
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 30rpx;
  color: #00C1B9;
}
</style>
